/*
*    Qizx/open 3.0 beta
*
* This code is the open-source version of Qizx.
* Copyright (C) 2004-2009 Axyana Software -- All rights reserved.
*
* The contents of this file are subject to the Mozilla Public License 
*  Version 1.1 (the "License"); you may not use this file except in 
*  compliance with the License. You may obtain a copy of the License at
*  http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
*  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
*  for the specific language governing rights and limitations under the
*  License.
*
* The Initial Developer of the Original Code is Xavier Franc - Axyana Software.
*
*/
using System;
using EvaluationException = System.Qizx.Api.EvaluationException;
using XMLPushStreamBase = System.Qizx.Xdm.XMLPushStreamBase;
using EvalContext = System.Qizx.XQuery.EvalContext;
using ExprDisplay = System.Qizx.XQuery.ExprDisplay;
using Focus = System.Qizx.XQuery.Focus;
using ModuleContext = System.Qizx.XQuery.ModuleContext;
using XQItem = System.Qizx.XQuery.XQItem;
using XQValue = System.Qizx.XQuery.XQValue;
namespace System.Qizx.XQuery.Op
{
	
	public class IfExpr:Expression
	{
		public override int Flags
		{
			get
			{
				return (UpdatingExpr.isUpdating(exprThen) || UpdatingExpr.isUpdating(exprElse))?UPDATING:0;
			}
			
		}
		public Expression cond;
		public Expression exprThen, exprElse;
		
		public IfExpr(Expression cond, Expression exprThen, Expression exprElse)
		{
			this.cond = cond;
			this.exprThen = exprThen;
			this.exprElse = exprElse;
		}
		
		public override void  Dump(ExprDisplay d)
		{
			d.header(this);
			d.child("cond", cond);
			d.child("then", exprThen);
			d.child("else", exprElse);
		}
		
		public override Expression Child(int rank)
		{
			switch (rank)
			{
				
				case 0: 
					return cond;
				
				case 1: 
					return exprThen;
				
				case 2: 
					return exprElse;
				}
			return null;
		}
		
		public override Expression StaticCheck(ModuleContext context, int flags)
		{
			cond = context.staticCheck(cond, 0);
			exprThen = context.simpleStaticCheck(exprThen, 0);
			exprElse = context.simpleStaticCheck(exprElse, 0);
			
			type = exprThen.getType().unionWith(exprElse.getType(), true);
			
			if (exprThen.isUpdating() != exprElse.isUpdating())
				if (!(UpdatingExpr.isNeutral(exprThen) || UpdatingExpr.isNeutral(exprElse)))
					context.error("XUST0001", this, "mix of updating and non-updating expressions in if");
			return this;
		}
		
		public override XQValue eval(Focus focus, EvalContext context)
		{
			context.at(this);
			if (cond.evalEffectiveBooleanValue(focus, context))
			{
				return exprThen.eval(focus, context);
			}
			else
			{
				// System.err.println("else");
				return exprElse.eval(focus, context);
			}
		}
		
		public override long evalAsInteger(Focus focus, EvalContext context)
		{
			context.at(this);
			if (cond.evalEffectiveBooleanValue(focus, context))
			{
				return exprThen.evalAsInteger(focus, context);
			}
			else
			{
				return exprElse.evalAsInteger(focus, context);
			}
		}
		
		public override long evalAsOptInteger(Focus focus, EvalContext context)
		{
			context.at(this);
			if (cond.evalEffectiveBooleanValue(focus, context))
			{
				return exprThen.evalAsOptInteger(focus, context);
			}
			else
			{
				return exprElse.evalAsOptInteger(focus, context);
			}
		}
		
		public override double evalAsDouble(Focus focus, EvalContext context)
		{
			context.at(this);
			if (cond.evalEffectiveBooleanValue(focus, context))
			{
				return exprThen.evalAsDouble(focus, context);
			}
			else
			{
				return exprElse.evalAsDouble(focus, context);
			}
		}
		
		public override double evalAsOptDouble(Focus focus, EvalContext context)
		{
			context.at(this);
			if (cond.evalEffectiveBooleanValue(focus, context))
			{
				return exprThen.evalAsOptDouble(focus, context);
			}
			else
			{
				return exprElse.evalAsOptDouble(focus, context);
			}
		}
		
		public override string evalAsString(Focus focus, EvalContext context)
		{
			context.at(this);
			if (cond.evalEffectiveBooleanValue(focus, context))
			{
				return exprThen.evalAsString(focus, context);
			}
			else
			{
				return exprElse.evalAsString(focus, context);
			}
		}
		
		public override XQItem evalAsItem(Focus focus, EvalContext context)
		{
			context.at(this);
			if (cond.evalEffectiveBooleanValue(focus, context))
			{
				return exprThen.evalAsItem(focus, context);
			}
			else
			{
				return exprElse.evalAsItem(focus, context);
			}
		}
		
		public override void  EvalAsEvents(XMLPushStreamBase output, Focus focus, EvalContext context)
		{
			context.at(this);
			if (cond.evalEffectiveBooleanValue(focus, context))
				exprThen.EvalAsEvents(output, focus, context);
			else
				exprElse.EvalAsEvents(output, focus, context);
		}
	}
}